package cn.mygweb.admin.modules.sys.service.impl;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;

import com.github.pagehelper.PageHelper;

import cn.mygweb.admin.modules.sys.mapper.UserMapper;
import cn.mygweb.admin.modules.sys.pojo.User;
import cn.mygweb.admin.modules.sys.service.UserService;
import tk.mybatis.mapper.entity.Example;

/** 
* <p>Title: SysUserServiceImpl.java</p>
* <p>Description: 系统用户service实现类</p>  
* @author gxb 
* @date 2019年6月1日 
* <p>Copyright: Copyright (c) 2019</p>
* <p>Company: www.mygweb.cn</p> 
*/
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;
	
	/**
	 * <p>Title: findByUsername</p>
	 * <p>Description: 通过用户名查找用户信息</p>
	 * @param username
	 * @return
	 * @see cn.mygweb.admin.modules.sys.service.UserService#findByUsername(java.lang.String)
	 */
	@Override
	@Transactional(propagation=Propagation.SUPPORTS)
	public User findByUsername(String username) {
		return userMapper.findByUsername(username);
	}
	
	/**
	 * <p>Title: insert</p>
	 * <p>Description: 添加用户信息</p>
	 * @param user
	 * @see cn.mygweb.admin.modules.sys.service.UserService#insert(cn.mygweb.admin.modules.sys.pojo.User)
	 */
	@Transactional(propagation=Propagation.REQUIRED)
	public void insert(User user) {
		userMapper.insertSelective(user);//没有指定的字段如果有默认值会保留默认值
	}

	/**
	 * <p>Title: updateUser</p>
	 * <p>Description: 更新用户信息</p>
	 * @param user
	 * @see cn.mygweb.admin.modules.sys.service.UserService#updateUser(cn.mygweb.admin.modules.sys.pojo.User)
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void updateUser(User user) {
		userMapper.updateByPrimaryKeySelective(user);// 根据主键更新属性不为null的值，推荐
	}

	/**
	 * <p>Title: deleteUser</p>
	 * <p>Description: 删除用户信息</p>
	 * @param id
	 * @see cn.mygweb.admin.modules.sys.service.UserService#deleteUser(java.lang.String)
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void deleteUser(String id) {
		userMapper.deleteByPrimaryKey(id);		
	}
	
	/**
	 * <p>Title: findById</p>
	 * <p>Description: 通过id查询用户信息</p>
	 * @param id
	 * @return
	 * @see cn.mygweb.admin.modules.sys.service.UserService#findById(java.lang.String)
	 */
	@Override
	@Transactional(propagation=Propagation.SUPPORTS)
	public User findById(String id) {
		return userMapper.findById(id);
	}

	/**
	 * <p>Title: queryUserList</p>
	 * <p>Description: 查询所有用户信息</p>
	 * @param user
	 * @return
	 * @see cn.mygweb.admin.modules.sys.service.UserService#queryUserList(cn.mygweb.admin.modules.sys.pojo.User)
	 */
	@Override
	@Transactional(propagation=Propagation.SUPPORTS)
	public List<User> queryUserList(User user) {
		
		Example example = new Example(User.class);
		example.setOrderByClause("regist_time desc");//根据注册时间降序排列，新注册的用户在最前面显示
		
		return userMapper.selectByExample(example);
	}

	/**
	 * <p>Title: userAlreadyExists</p>
	 * <p>Description: 判断用户是否存在</p>
	 * @param user
	 * @return
	 * @see cn.mygweb.admin.modules.sys.service.UserService#userAlreadyExists(cn.mygweb.admin.modules.sys.pojo.User)
	 */
	@Override
	@Transactional(propagation=Propagation.SUPPORTS)
	public boolean userAlreadyExists(User user) {
		if(null != user.getUsername()) {
			User validUser = userMapper.findByUsernameAndId(user.getUsername(), user.getId());
			return validUser != null;
		}
		return false;
	}

	/**
	 * <p>Title: queryPermsByUserId</p>
	 * <p>Description: 通过userId查询用户的权限信息</p>
	 * @param userId
	 * @return
	 * @see cn.mygweb.admin.modules.sys.service.UserService#queryPermsByUserId(java.lang.String)
	 */
	@Override
	public List<String> queryPermsByUserId(String userId) {
		return userMapper.queryPermsByUserId(userId);
	}

	/**
	 * <p>Title: getLoginUser</p>
	 * <p>Description: 获取当前登录的用户信息</p>
	 * @return
	 * @see cn.mygweb.admin.modules.sys.service.UserService#getLoginUser()
	 */
	@Override
	public void setLoginUser(ModelMap model) {
		Subject subject = SecurityUtils.getSubject();
		User user = (User)subject.getPrincipal();
		model.addAttribute("loginUser", user);
	}

	/**
	 * <p>Title: queryUserListPaged</p>
	 * <p>Description: 分页查询用户信息</p>
	 * @param user
	 * @param pageNum
	 * @param pageSize
	 * @return
	 * @see cn.mygweb.admin.modules.sys.service.UserService#queryUserListPaged(cn.mygweb.admin.modules.sys.pojo.User, java.lang.Integer, java.lang.Integer)
	 */
	@Override
	public List<User> queryUserListPaged(User user, Integer pageNum, Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		Example example = new Example(User.class);
		return userMapper.selectByExample(example);
	}

	/**
	 * <p>Title: selectCount</p>
	 * <p>Description: 查询信息的总条数</p>
	 * @param user
	 * @return
	 * @see cn.mygweb.admin.modules.sys.service.UserService#selectCount(cn.mygweb.admin.modules.sys.pojo.User)
	 */
	@Override
	public Integer selectCount(User user) {
		Example example = new Example(User.class);
		return userMapper.selectCountByExample(example);
	}

}
